<template>
  <div class="app-container">
    <tree-table :data="data" :columns="columns" border @selected="mockTableData">
      <el-table-column label="编码">
        <template slot-scope="scope">
          <span style="color:sandybrown">{{scope.row.dataCode}}</span>
        </template>
      </el-table-column>
      <el-table-column label="描述" width="200">
        <template slot-scope="scope">
          <span style="color:sandybrown">{{scope.row.dataComment}}</span>
        </template>
      </el-table-column>
      <el-table-column label="排序" width="120">
        <template slot-scope="scope">
          <span style="color:sandybrown">{{scope.row.orders}}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否有效" width="120">
        <template slot-scope="scope">
          <span style="color:sandybrown">{{scope.row.isValid == '1' ? '是' : '否'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <div style="text-align:center">
            <i class="el-icon-circle-plus" @click="addData(scope.row)"
               style="margin-right:10px;color:black;cursor:pointer"></i>
            <i class="el-icon-edit" @click="editData(scope.row)" v-show="!scope.row.root"
               style="margin-left:10px;margin-right:10px;color:black;cursor:pointer"></i>
            <i class="el-icon-delete" @click="deleteData(scope.row)" v-show="!scope.row.root"
               style="margin-left:10px;color:black;cursor:pointer"></i>
          </div>
        </template>
      </el-table-column>
    </tree-table>


    <el-dialog :title='menuDataName' :visible.sync="dialogShow" center :before-close="preClose" width="35%">
      <hr style="height:0.5px;border:none;border-top:1px solid #CCC;margin:-15px 0px 25px 0px">
      <el-form :model="form" ref="form" :rules="rules">
        <el-form-item label="编码" :label-width="formLabelWidth" prop="dataCode">
          <el-input v-model="form.dataCode" placeholder="编码名称"></el-input>
        </el-form-item>
        <el-form-item label="名称" :label-width="formLabelWidth" prop="dataName">
          <el-input v-model="form.dataName"></el-input>
        </el-form-item>
        <el-form-item label="描述" :label-width="formLabelWidth">
          <el-input v-model="form.dataComment"></el-input>
        </el-form-item>
        <el-form-item label="排序" :label-width="formLabelWidth" prop="orders">
          <el-input v-model="form.orders"></el-input>
        </el-form-item>
        <el-form-item label="是否有效" :label-width="formLabelWidth" prop="isValid">
          <el-select v-model="form.isValid" placeholder="选择是否有效">
            <el-option label="是" value="1"></el-option>
            <el-option label="否" value="0"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
                <el-button @click="closeDialog">取  消</el-button>
                <el-button type="primary" @click="confirmDialog('form')">提  交</el-button>
            </span>
    </el-dialog>
  </div>
</template>

<script>
  import treeTable from '@/components/TreeTable'
  import {mapGetters} from 'vuex';
  export default {
    name: 'menuTree',
    components: {
      treeTable
    },
    data() {
      return {
        data: [],
        menuDataName: '',
        formLabelWidth: '80px',
        dialogShow: false,
        form: {},
        node: {},
        rules: {
          dataCode: [
            {required: true, message: '请输入编码', trrigger: 'blur'}
          ],
          dataName: [
            {required: true, message: '请输入名称', trigger: 'blur'}
          ],
          orders: [
            {required: true, message: '请输入排序', trigger: 'blur'}
          ],
          isValid: [
            {required: true, message: '请选择排序', trigger: 'blur'}
          ]
        }
      }
    },
    computed: {
      columns(){
        let cols = [];
        cols.push({
          text: '名称',
          align: 'center',
          value: 'dataName',
          width: 300
        });
        return cols;
      }
    },
    methods: {

      addData(node){
        this.node = node;
        this.form = {};
        this.form.parentId = node.dbid;
        this.form.isDel =false;
        this.dialogShow = true;

      },
      editData(node){
        this.node = node.parent;
        this.form = {};
        this.form.parentId = node.dbid;

        this.$store.dispatch("loadData", node.dbid).then(res => {
          this.form = this.$store.state.databook.dataBookEdit
        })
        this.dialogShow = true;

      },
      deleteData(node){
          this.node = node.parent;
        this.$confirm("确定要删除选中的数据?").then(res => {
          let id = node.dbid;
          this.$store.dispatch('deleteDataBook', id).then(res => {
            this.$message("操作成功")
            this.reloadData();
          })
        })


      },
      preClose(){
        this.$confirm('确认关闭？')
          .then(_ => {
            this.dialogShow = false;
          }).catch(_ => {
        });
      },

      message(data){

      },
      closeDialog(){
        this.$confirm('确认关闭此窗口？')
          .then(_ => {
            this.dialogShow = false;
          }).catch(_ => {
        });
      },
      confirmDialog(){
        this.$refs.form.validate(valid => {
          if (valid) {
            this.$confirm("确认提交").then(res => {
              this.$store.dispatch("SaveDataBook", this.form).then(res => {
                this.dialogShow = false;
                this.reloadData();
              })


            })
          } else {
          }
        })


      },

      mockTableData(node){
        let id;
        if (node == null) {
          id = '';
        } else {
          id = node.dbid;
        }

        if (id == '') {
          this.$store.dispatch('databookList', id).then(() => {
            this.data = this.$store.state.databook.dataBookList;
          })
        } else {
          this.$store.dispatch('databookList', id).then(() => {
            let d = this.$store.state.databook.dataBookList;
            this.$set(node, 'children', d)
          })

        }
      },
      reloadData(){
        if (this.node.children) {
          this.node.children.splice(0, this.node.children);
        }
        this.mockTableData(this.node);
      }

    },

    created(){
      this.mockTableData(null)

    }
  }
</script>
<style>
  .el-table th > .cell {
    position: relative;
    word-wrap: normal;
    text-overflow: ellipsis;
    vertical-align: middle;
    width: 100%;
    box-sizing: border-box;
    text-align: center !important;
  }
</style>
